﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Xejen.Communication
{
    /// <summary>
    /// 表示通讯序列化配置
    /// </summary>
    /// <creator>marc</creator>
    public sealed class CommunicationSerializerOptions
    {
        /// <summary>
        /// 美观的序列化配置：
        /// <para>
        /// 1.序列化时命名方式使用CamelCase    <br/>
        /// 2.反序列化时忽略属性名称的大小写   <br/>
        /// 3.防止中文被编码为Unicode          <br/>
        /// </para>
        /// </summary>
        public static JsonSerializerOptions Normalize
        {
            get
            {
                JsonSerializerOptions options = new JsonSerializerOptions
                {
                    WriteIndented = true,
                    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                    PropertyNameCaseInsensitive = true,
                    Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
                };

                return options;
            }
        }

        /// <summary>
        /// 用于压缩成一行时使用
        /// </summary>
        public static JsonSerializerOptions Compress
        {
            get
            {
                JsonSerializerOptions options = new JsonSerializerOptions
                {
                    WriteIndented = false,
                    PropertyNameCaseInsensitive = false,
                    Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
                    Converters = { new DateTimeConverter("yyyy-MM-dd HH:mm:ss"), new DateTimeNullableConverter("yyyy-MM-dd HH:mm:ss") },
                };

                return options;
            }
        }

        /// <inheritdoc cref="CommunicationSerializerOptions"/>
        private CommunicationSerializerOptions() { }
    }
}
